// pages/order/invoice.js
const App = getApp()
import Util from '../../utils/util'

Page({
  data: {
    init: false,


    type: 1,
    name: '',
    person: true,
    taxno: '',
    bank: '',
    card: '',
    addr: '',
    phone: '',
    ele: true,
    email: '',
    post: ''
  },
  onLoad() {
    this.getOpenerEventChannel().on('current', (data) => {
      if (data && data.name) {
        this.setData({
          init: true,
          type: data.type,
          name: data.name,
          person: data.person,
          taxno: data.taxno,
          bank: data.bank,
          card: data.card,
          addr: data.addr,
          phone: data.phone,
          ele: data.ele,
          email: data.email,
          post: data.post
        })
      } else {
        this.execLoad()
      }
    })
  },
  doEnter() {
    if (!this.data.init) {
      return
    }
    if (!this.data.name) {
      Util.Toast.fail('请填写抬头名称')
      return
    }
    if (!this.data.person) {
      if (!this.data.taxno) {
        Util.Toast.fail('请填写单位税号')
        return
      }
    }
    if (this.data.ele) {
      if (!this.data.email) {
        Util.Toast.fail('请填写邮箱地址')
        return
      }
    } else {
      if (!this.data.post) {
        Util.Toast.fail('请填写收件地址')
        return
      }
    }
    let invoice = {
      type: this.data.type,
      name: this.data.name,
      person: this.data.person,
      taxno: this.data.taxno,
      bank: this.data.bank,
      card: this.data.card,
      addr: this.data.addr,
      phone: this.data.phone,
      ele: this.data.ele,
      email: this.data.email,
      post: this.data.post
    }

    App.doPost({
      uri: '/app/wallet/invoice/save',
      body: {
        invoice: invoice
      },
      fnSucc: () => {
        this.getOpenerEventChannel().emit('enter', invoice)
        wx.navigateBack({
          delta: 1,
        })
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      }
    })

  },
  execLoad() {
    App.doPost({
      uri: '/app/wallet/invoice/config',
      body: {},
      fnSucc: resp => {
        this.setData({
          init: true,
          type: resp.data.invoice.type || 1,
          name: resp.data.invoice.name,
          person: resp.data.invoice.person == null ? true : resp.data.invoice.person,
          taxno: resp.data.invoice.taxno,
          bank: resp.data.invoice.bank,
          card: resp.data.invoice.card,
          addr: resp.data.invoice.addr,
          phone: resp.data.invoice.phone,
          ele: resp.data.invoice.ele == null ? true : resp.data.invoice.ele,
          email: resp.data.invoice.email,
          post: resp.data.invoice.post
        })
      }
    })
  },
  doTypeChange(e) {
    this.setData({
      type: parseInt(e.currentTarget.dataset.type)
    })
  },

  doPersonChange(e) {
    this.setData({
      person: e.currentTarget.dataset.type === 'true'
    })
  },
  doEleChange(e) {
    this.setData({
      ele: e.currentTarget.dataset.type === 'true'
    })
  },
})